unit UnDataModel;

interface

uses
  UnDmDataSets,Classes,SysUtils,Db;

type
  TConfiguratie = class
  private
    aDataModule : TDmDatasets;
    aInterval1   : Integer;
    aSensorType1 : Integer;
    aInterval2   : Integer;
    aSensorType2 : Integer;
    aInterval3   : Integer;
    aSensorType3 : Integer;
    aInterval4   : Integer;
    aSensorType4 : Integer;
    aInterval5   : Integer;
    aSensorType5 : Integer;
    aInterval6   : Integer;
    aSensorType6 : Integer;
    aCongiguratieId : integer;
    aConfiguratieSeq : Integer;
    aSequence : integer;
    aStamp : TDateTime;
  public
    procedure Save;
    procedure IncreaseSequence;
    constructor Create(aDm : TDmDatasets);
  published
    property Interval1 : Integer read aInterval1 write aInterval1;
    property Interval2 : Integer read aInterval2 write aInterval2;
    property Interval3 : Integer read aInterval3 write aInterval3;
    property Interval4 : Integer read aInterval4 write aInterval4;
    property Interval5 : Integer read aInterval5 write aInterval5;
    property Interval6 : Integer read aInterval6 write aInterval6;
    property SensorType1 : integer read aSensorType1 write aSensorType1;
    property SensorType2 : integer read aSensorType2 write aSensorType2;
    property SensorType3 : integer read aSensorType3 write aSensorType3;
    property SensorType4 : integer read aSensorType4 write aSensorType4;
    property SensorType5 : integer read aSensorType5 write aSensorType5;
    property SensorType6 : integer read aSensorType6 write aSensorType6;
    property CongiguratieId : integer read aCongiguratieId write aCongiguratieId;
    property Sequence : integer read aSequence write aSequence;
    property Stamp : TDateTime read aStamp write aStamp;
  end;

type
  TModule = class
  private
    aDataModule : TDmDatasets;
    aConfiguratie : TConfiguratie;
    aModuleId : integer;
    aLokatieId : integer;
    aModuleNummer : integer;
    aOnderzoekId : integer;
    aEnergie : integer;
    aLastId : integer;
  public
    procedure Save;
    constructor create(ModuleNummer : integer;aDm : TDmDatasets);
  published
    property ModuleId : Integer read aModuleId write aModuleId;
    property Configuratie : TConfiguratie read aConfiguratie write aConfiguratie;
    property Energrie : Integer read aEnergie write aEnergie;
    property LastId : Integer read aLastId write aLastId;
    property LokatieId : integer read aLokatieId write aLokatieId;
    property OnderzoekId : Integer read aOnderzoekId write aOnderzoekId;
  end;

type
  TMeting = class
  private
    aModule : TModule;
    aDataModule : TDmDatasets;
    aBerichtNummer : integer;
    aMeetWaarde : integer;
    aTypeId : integer;
    aOnderzoekId : integer;
    aModuleId : integer;
    aLokatieId : integer;
    aStamp : TDateTime;
  public
    procedure save;
    constructor create(ModuleNummer : integer;aDm : TDmDatasets);
  published
    property Module : TModule read aModule write aModule;
end;

implementation

////////////////////////////////////////////////////////////////////////////////
///  TModule
////////////////////////////////////////////////////////////////////////////////

constructor TModule.create(ModuleNummer : integer; aDm : TDmDatasets);
begin
  ModuleNummer := ModuleNummer;
  aDm.QrModule.Open;
  if aDm.QrModule.Locate('NUMMER',ModuleNummer,[loCaseInsensitive]) then
  begin
    aModuleId := aDm.QrModuleId.AsInteger;
    aConfiguratie := TConfiguratie.Create(aDataModule);
    aEnergie := aDm.QrModuleENERGIE.AsInteger;
    aLastId := aDm.QrModuleLAST_ID.AsInteger;
    aOnderzoekId := aDm.QrModuleONDERZOEKS_ID.AsInteger;
  end
  else
  begin
    aModuleId := 0;
    aLokatieId := 0;
    aOnderzoekId := 0;
    aLastId := 0;
    aEnergie := 0;
  end;
end;

procedure TModule.Save;
begin
  if aModuleId = 0 then aDataModule.QrModule.insert else
  aDataModule.QrModule.edit;
  with aDataModule do
  begin
     QrModuleNUMMER.AsInteger := aModuleNummer;
     QrModuleLOKATIE_ID.AsInteger := aLokatieId;
     QrModuleONDERZOEKS_ID.AsInteger := aOnderzoekId;
     QrModuleENERGIE.AsInteger := aEnergie;
     QrModuleLAST_ID.AsInteger := aLastId;
  end;
  aConfiguratie.Save;
  aDataModule.QrModule.Post;
end;

////////////////////////////////////////////////////////////////////////////////
///  TConfiguratie
////////////////////////////////////////////////////////////////////////////////

constructor TConfiguratie.Create(aDm : TDmDatasets);
begin
  aDataModule := aDm;
  if aDataModule.TblConfig.RecordCount > 0 then
  begin
    with aDataModule do
    begin
      aInterval1 := TblConfigINTERVAL_1.AsInteger;
      aSensorType1 := TblConfigTYPE_1.AsInteger;
      aInterval2 := TblConfigINTERVAL_1.AsInteger;
      aSensorType2 := TblConfigTYPE_2.AsInteger;
      aInterval3 := TblConfigINTERVAL_1.AsInteger;
      aSensorType3 := TblConfigTYPE_3.AsInteger;
      aInterval4 := TblConfigINTERVAL_1.AsInteger;
      aSensorType4 := TblConfigTYPE_4.AsInteger;
      aInterval5 := TblConfigINTERVAL_1.AsInteger;
      aSensorType5 := TblConfigTYPE_5.AsInteger;
      aInterval6 := TblConfigINTERVAL_1.AsInteger;
      aSensorType6 := TblConfigTYPE_6.AsInteger;
      aCongiguratieId := TblConfigID.AsInteger;
      aConfiguratieSeq := TblConfigSEQUENCE.AsInteger;
      aStamp := TblConfigSTAMP.AsDateTime;
    end;
  end
  else
  begin
    aInterval1 := 0;
    aSensorType1 := 0;
    aInterval2 := 0;
    aSensorType2 := 0;
    aInterval3 := 0;
    aSensorType3 := 0;
    aInterval4 := 0;
    aSensorType4 := 0;
    aInterval5 := 0;
    aSensorType5 := 0;
    aInterval6 := 0;
    aSensorType6 := 0;
    aCongiguratieId := 0;
    aConfiguratieSeq := 0;
  end;
end;

procedure TConfiguratie.IncreaseSequence;
begin
  aConfiguratieSeq := aConfiguratieSeq + 1;
  aConfiguratieSeq := AConfiguratieSeq mod 255;
end;

procedure TConfiguratie.Save;
begin
  if aDataModule.TblConfig.RecordCount > 0 then
  begin
    with aDataModule do
    begin
      TblConfig.Edit;
      TblConfigINTERVAL_1.AsInteger := aInterval1;
      TblConfigTYPE_1.AsInteger := aSensorType1;
      TblConfigINTERVAL_2.AsInteger := aInterval2;
      TblConfigTYPE_2.AsInteger := aSensorType2;
      TblConfigINTERVAL_3.AsInteger := aInterval3;
      TblConfigTYPE_3.AsInteger := aSensorType3;
      TblConfigINTERVAL_4.AsInteger := aInterval4;
      TblConfigTYPE_4.AsInteger := aSensorType4;
      TblConfigINTEGER_5.AsInteger := aInterval5;
      TblConfigTYPE_5.AsInteger := aSensorType5;
      TblConfigINTEGER_6.AsInteger := aInterval6;
      TblConfigTYPE_6.AsInteger := aSensorType6;
      aSequence := aSequence + 1;
      aSequence := aSequence mod 100;
      TblConfigSEQUENCE.AsInteger := aSequence;
      aStamp := TblConfigSTAMP.AsDateTime;
    end;
  end;
end;
////////////////////////////////////////////////////////////////////////////////
/// Meting
////////////////////////////////////////////////////////////////////////////////

constructor TMeting.create(ModuleNummer : integer;aDm : TDmDatasets);
begin
  aDataModule := aDm;
  aModule := TModule.create(ModuleNummer,aDataModule);
end;

procedure TMeting.save;
begin
  sleep(0);
end;

end.
